layui.config({
    base : "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index','form','jquery', 'table'],function() {
    var form = layui.form,
        table = layui.table,
        admin = layui.admin;

    var quota_apply =false;

    table.render({
        elem: '#oem_quota_apply_table'
        ,url: '/quota_apply_oem/queryData'
        ,method:"post"
        ,height: 500
        ,limit:50							//显示数目
        ,limits:[50,100]
        ,cols: [[
            {field:'levelId',width:200, title: '等级ID',"align":"left"}
            ,{field:'levelName',width:200, title: '等级名称',"align":"left"}
            ,{field:'quantity',width:200, title: '数量',"align":"left"}
            ,{field:'applyUserName',width:200, title: '申请人',"align":"left"}
            ,{field:'createdTime',width:200, title: '申请时间',"align":"left"}
            ,{field:'updatedTime',width:200, title: '审批时间',"align":"left"}
            ,{field:'message',width:200, title: '备注',"align":"left"}

            ,{field:'status',width:200, title: '状态',"align":"left",templet:function (d) {

                    if(d.status==1){
                        return "已通过";
                    }else if(d.status==-1){
                        return "已驳回"
                    }else {
                        return "未处理";
                    }
                }},
            ,{field:'OPT', width:300, title: '操作',"align":"center",toolbar:"#OptData"}

        ]]
        ,request:{
            pageName: 'pageNo' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where: {
            applyUserName:$(".queryTable input[name='applyUserName']").val(),
            status:$(".queryTable select[name='status']").val()
        }
        ,id: 'oem_quota_apply_table'
        ,page: true
    });

   table.on('tool(oem_quota_apply_table)', function(obj){
       quota_apply = false;
        var table_event = obj.event;

        var data = obj.data;

        if(data.status==0){
            if(table_event=="apply_do"){

                admin.popup({
                    title: '申请处理'
                    ,shade: 0
                    ,anim: -1
                    ,offset:"20px"
                    ,area: ['80%', '500px']
                    ,id: 'layadmin-layer-skin-modify'
                    ,skin: 'layui-anim layui-anim-upbit'
                    ,content:$("#apply_do_win")
                    ,end:function () {

                    }
                });

                form.val('apply_do_form', {
                    "id":data.id
                });

            }
        }else {
            layer.msg("该条数据已经处理",{
                offset: '32%'
                ,icon: 2
                ,time: 3000
            },function () {
                layer.closeAll();
            })
        }


    });

    form.on('submit(apply_do_ok)',function (data) {
        if(!quota_apply){
            quota_apply = true;
            $.ajax({
                url:"/quota_apply_oem/apply_handle",
                data:data.field,
                type:"post",
                success:function (result) {
                    if (result.code == 1){
                        layer.msg("提交成功",{
                            offset: '32%'
                            ,icon: 1
                            ,time: 3000
                            ,btn: '知道了'
                            ,btnAlign:"c"
                            ,shade:0.4
                            ,yes:function () {
                                table.reload('oem_quota_apply_table', {
                                });
                                layer.closeAll();
                            }
                        })
                    }else{
                        quota_apply =false;
                        layer.msg(result.message,{
                            offset: '32%'
                            ,icon: 2
                            ,time: 3000
                        },function () {
                            layer.closeAll();
                        })
                    }

                }
            });
        }else {
            layer.msg("不能重复提交表单",{
                offset: '32%'
                ,icon: 2
                ,time: 3000
            },function () {

            })
        }

        return false;
    });

    $(".search_btn").click(function () {

        table.reload('oem_quota_apply_table', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
                applyUserName:$(".queryTable input[name='applyUserName']").val(),
                status:$(".queryTable select[name='status']").val()
            }
        });

    });


});